using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media.Imaging;
using Common;
using Common.Enum;
using Microsoft.Win32;
using SProject.BasicInformation;
using SSettings;

namespace ProjectView.ProjectControls
{
	public partial class UCProjectTarget : UserControl, IComponentConnector, IStyleConnector
	{
		public static readonly DependencyProperty TypeProperty = DependencyProperty.Register("Type", typeof(EProjectType), typeof(UCProjectTarget), new UIPropertyMetadata(EProjectType.AQ));

		public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register("IsReadOnly", typeof(bool), typeof(UCProjectTarget), new PropertyMetadata(false, OnIsReadOnlyChanged));

		public static readonly DependencyProperty TargetProperty = DependencyProperty.Register("Target", typeof(Target), typeof(UCProjectTarget), new PropertyMetadata(null, OnTargetChanged));

		private ObservableCollection<AQTargetDataRow> _ListAQTarget = new ObservableCollection<AQTargetDataRow>();

		private ObservableCollection<GridCheckBoxCell> _CheckCellList = new ObservableCollection<GridCheckBoxCell>();

		private ChannelSetting _ChannelSetting = ConfigSettings.GetInstance().ChannelSetting;

		private string _StringNA = Utility.GetMessage("NA");

		private int[] _Channels = new int[8] { 1, 2, 3, 4, 0, 0, 0, 0 };

		public AQTargetDataRow RowDye = new AQTargetDataRow();

		public AQTargetDataRow RowCheckBox = new AQTargetDataRow();

		public AQTargetDataRow RowImage = new AQTargetDataRow();

		private int DataRowIndex;

		private bool isMutilpleTarget;

		public static RoutedUICommand SelectImageCommand = new RoutedUICommand("SelectImageCommand", "SelectImageCommand", typeof(UCProjectTarget));

		private int tubeCount = 1;

		private int targetCount = 1;

		public DataGrid GridControl
		{
			get
			{
				return dataGrid;
			}
		}

		public bool IsReadOnly
		{
			get
			{
				return (bool)GetValue(IsReadOnlyProperty);
			}
			set
			{
				SetValue(IsReadOnlyProperty, value);
			}
		}

		public EProjectType Type
		{
			get
			{
				return (EProjectType)GetValue(TypeProperty);
			}
			set
			{
				SetValue(TypeProperty, value);
			}
		}

		public Target Target
		{
			get
			{
				return (Target)GetValue(TargetProperty);
			}
			set
			{
				SetValue(TargetProperty, value);
			}
		}

		public ObservableCollection<AQTargetDataRow> ListAQTarget
		{
			get
			{
				return _ListAQTarget;
			}
			set
			{
				_ListAQTarget = value;
			}
		}

		public bool IsVisibleImage { get; set; }

		public Thickness ComboxMargin { get; set; }

		public ObservableCollection<GridCheckBoxCell> CheckCellList
		{
			get
			{
				return _CheckCellList;
			}
			set
			{
				_CheckCellList = value;
			}
		}

		public List<ObservableCollection<DyeItem>> ChannelDyes { get; set; }

		public List<ListCollectionView> ChannelDyesView { get; set; }

		public List<string> DefaultTargetNames { get; set; }

		private static void OnIsReadOnlyChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
		}

		private static void OnTargetChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			(sender as UCProjectTarget).SetTargetToWin();
		}

		public UCProjectTarget()
		{
			InitializeComponent();
			isMutilpleTarget = ConfigReader.GetInstance().GetMutilpleTarget();
			string[] array = ConfigReader.GetInstance().GetChannels().Split('&');
			Array.Resize(ref _Channels, ConfigReader.GetInstance().GetProjectMaxChannelCount());
			ChannelDyes = new List<ObservableCollection<DyeItem>>();
			ChannelDyesView = new List<ListCollectionView>();
			for (int i = 0; i < _Channels.Count(); i++)
			{
				_Channels[i] = Convert.ToInt32(array[i]);
				ChannelDyes.Add(new ObservableCollection<DyeItem>());
				ChannelDyesView.Add(new ListCollectionView(ChannelDyes[i]));
				ChannelDyesView[i].GroupDescriptions.Add(new PropertyGroupDescription("Category"));
			}
			DefaultTargetNames = new List<string> { _StringNA };
			for (int j = 0; j < _Channels.Count(); j++)
			{
				GridCheckBoxCell gridCheckBoxCell = new GridCheckBoxCell
				{
					Value = false
				};
				gridCheckBoxCell.IsEnable = _ChannelSetting.AllChannelProperty[_Channels[j]].IsEnabled;
				gridCheckBoxCell.Index = j + 1;
				gridCheckBoxCell.PropertyChanged += checkCell_PropertyChanged;
				CheckCellList.Add(gridCheckBoxCell);
			}
			RowCheckBox.ColumnItem = Utility.GetMessage("Prj_Select");
			for (int k = 0; k < _Channels.Count(); k++)
			{
				RowCheckBox.SetValue(k + 1, CheckCellList[k]);
			}
			_ListAQTarget.Add(RowCheckBox);
			RowDye.ColumnItem = Utility.GetMessage("Prj_Dye");
			for (int l = 0; l < _Channels.Count(); l++)
			{
				RowDye.SetValue(l + 1, _ChannelSetting.AllChannelProperty[_Channels[l]].DyeList[0]);
			}
			_ListAQTarget.Add(RowDye);
			_ListAQTarget.Add(new AQTargetDataRow(Utility.GetMessage("Prj_TubeDispaly") + 1, "", "", "", "", "", "", "", "")
			{
				TubeNo = 1
			});
			DataRowIndex = _ListAQTarget.Count - 1;
			for (int m = 1; m <= _Channels.Count(); m++)
			{
				DataGridTemplateColumn item = new DataGridTemplateColumn
				{
					Header = m,
					CellTemplateSelector = new DataGridCellSelector(),
					CellEditingTemplateSelector = new DataGridCellEditSelector()
				};
				dataGrid.Columns.Add(item);
			}
			dataGrid.ItemsSource = _ListAQTarget;
			base.CommandBindings.Add(new CommandBinding(SelectImageCommand, SelectImageCommandExecute));
			IsVisibleImage = ConfigReader.GetInstance().GetProjectImageVisible();
			if (IsVisibleImage)
			{
				ComboxMargin = new Thickness(37.0, 0.0, 0.0, 0.0);
			}
			else
			{
				ComboxMargin = new Thickness(20.0, 0.0, 0.0, 0.0);
			}
		}

		private void checkCell_PropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			GridCheckBoxCell gridCheckBoxCell = sender as GridCheckBoxCell;
			if (gridCheckBoxCell == null || !e.PropertyName.Equals("Value"))
			{
				return;
			}
			if (!IsReadOnly && !gridCheckBoxCell.Value && MessageBox.Show(Utility.GetMessage("Msg_Modify_Delete_Info_Confirm"), Utility.GetMessage("Warning"), MessageBoxButton.YesNo, MessageBoxImage.Exclamation) == MessageBoxResult.No)
			{
				gridCheckBoxCell.Value = true;
				return;
			}
			List<GridCheckBoxCell> list = null;
			switch (Type)
			{
			case EProjectType.HRM:
				list = CheckCellList.Where((GridCheckBoxCell s) => s.Value).ToList();
				if (list.Count > 1)
				{
					foreach (GridCheckBoxCell item in list)
					{
						if (gridCheckBoxCell != item)
						{
							item.PropertyChanged -= checkCell_PropertyChanged;
							item.Value = false;
							item.PropertyChanged += checkCell_PropertyChanged;
							_ListAQTarget[DataRowIndex].SetValue(item.Index, "");
							_ListAQTarget[DataRowIndex].SetColor(item.Index, Utility.GetTargetColor(1, item.Index));
						}
					}
				}
				gridCheckBoxCell.PropertyChanged -= checkCell_PropertyChanged;
				gridCheckBoxCell.Value = true;
				gridCheckBoxCell.PropertyChanged += checkCell_PropertyChanged;
				break;
			case EProjectType.AD:
				list = CheckCellList.Where((GridCheckBoxCell s) => s.Value).ToList();
				if (list.Count > 2)
				{
					gridCheckBoxCell.PropertyChanged -= checkCell_PropertyChanged;
					gridCheckBoxCell.Value = false;
					gridCheckBoxCell.PropertyChanged += checkCell_PropertyChanged;
				}
				break;
			}
			if (gridCheckBoxCell.Value)
			{
				dataGrid.CurrentColumn = dataGrid.Columns[gridCheckBoxCell.Index];
				dataGrid.CurrentItem = _ListAQTarget[DataRowIndex];
				for (int i = DataRowIndex; i < _ListAQTarget.Count; i++)
				{
					_ListAQTarget[i].SetValue(gridCheckBoxCell.Index, _ListAQTarget[i].TubeNo + "-" + gridCheckBoxCell.Index + ((!isMutilpleTarget) ? "" : ("-" + _ListAQTarget[i].TargetIndex)));
				}
				dataGrid.BeginEdit();
			}
			else
			{
				ClearData(gridCheckBoxCell.Index);
			}
		}

		private void ClearData(int idx)
		{
			for (int i = DataRowIndex; i < _ListAQTarget.Count; i++)
			{
				_ListAQTarget[i].SetValue(idx, "");
				_ListAQTarget[i].SetColor(idx, Utility.GetTargetColor(i - DataRowIndex + 1, idx));
			}
		}

		private void SetTargetToWin()
		{
			dataGrid.CancelEdit();
			if (Target != null)
			{
				int num = Target.GetTubeCount();
				if (num > 0)
				{
					if (Target.GetTargetCount() > 0)
					{
						SetTargetCount(Target.GetTargetCount());
					}
					else
					{
						SetTargetCount(targetCount);
					}
					SetTubeCount(num);
					string[] array = null;
					array = (string.IsNullOrEmpty(Target.Channels) ? ConfigReader.GetInstance().GetChannels().Split('&') : Target.Channels.Split('&'));
					int i;
					for (i = 1; i <= _Channels.Count(); i++)
					{
						GridCheckBoxCell gridCheckBoxCell = RowCheckBox.GetObject(i) as GridCheckBoxCell;
						IEnumerable<TargetItem> source = Target.Items.Where((TargetItem item) => item.ChannelNo.ToString().Equals(i.ToString()));
						gridCheckBoxCell.Value = source.Count() > 0;
						if (gridCheckBoxCell.Value || (i <= array.Count() && array[i - 1] != "0"))
						{
							gridCheckBoxCell.IsEnable = true;
							for (int j = DataRowIndex; j < _ListAQTarget.Count; j++)
							{
								_ListAQTarget[j].SetValue(i, _StringNA);
								_ListAQTarget[j].SetColor(i, Utility.GetTargetColor(j - DataRowIndex + 1, i));
							}
						}
						if (i <= array.Count() && array[i - 1] != "0")
						{
							RowDye.SetValue(i, _ChannelSetting.AllChannelProperty[int.Parse(array[i - 1])].DyeList[0]);
						}
						else
						{
							RowDye.SetValue(i, string.Empty);
						}
					}
					List<TargetItem> list = (from s in Target.Items
						orderby s.TubeNo, s.TargetIndex
						select s).ToList();
					int num2 = DataRowIndex;
					int num3 = 1;
					int num4 = 1;
					int k = 1;
					for (int l = 0; l < list.Count; l++)
					{
						TargetItem targetItem = list[l];
						_ListAQTarget[1].SetValue(targetItem.ChannelNo, targetItem.Dye);
						if (num3 != targetItem.TubeNo)
						{
							num2++;
							for (; k < targetCount; k++)
							{
								num2++;
							}
							k = 1;
						}
						else if (num4 != targetItem.TargetIndex)
						{
							num2++;
							k++;
						}
						num4 = targetItem.TargetIndex;
						num3 = targetItem.TubeNo;
						_ListAQTarget[num2].ColumnItem = targetItem.TubeName;
						_ListAQTarget[num2].SetValue(targetItem.ChannelNo, targetItem.Name);
						_ListAQTarget[num2].SetColor(targetItem.ChannelNo, targetItem.Color);
						if (targetItem.ImageData != null && targetItem.ImageData.Length != 0)
						{
							_ListAQTarget[num2].SetImage(targetItem.ChannelNo, Utility.ByteArrayToBitmapImage(targetItem.ImageData), targetItem.ImageData);
						}
						else
						{
							_ListAQTarget[num2].SetImage(targetItem.ChannelNo, null, new byte[0]);
						}
					}
					return;
				}
			}
			SetTubeCount(1);
			SetTargetCount(1);
			for (int m = 1; m <= _Channels.Count(); m++)
			{
				GridCheckBoxCell gridCheckBoxCell2 = RowCheckBox.GetObject(m) as GridCheckBoxCell;
				if (gridCheckBoxCell2 != null)
				{
					gridCheckBoxCell2.PropertyChanged -= checkCell_PropertyChanged;
					gridCheckBoxCell2.Value = false;
					gridCheckBoxCell2.PropertyChanged += checkCell_PropertyChanged;
					for (int n = DataRowIndex; n < _ListAQTarget.Count; n++)
					{
						_ListAQTarget[n].SetValue(gridCheckBoxCell2.Index, "");
						_ListAQTarget[n].SetColor(gridCheckBoxCell2.Index, Utility.GetTargetColor(n - DataRowIndex + 1, gridCheckBoxCell2.Index));
					}
				}
			}
		}

		public bool SaveTarget(bool showMsg = true)
		{
			new StringBuilder();
			if (showMsg)
			{
				dataGrid.CommitEdit();
			}
			for (int num = Target.Items.Count - 1; num >= 0; num--)
			{
				TargetItem targetItem = Target.Items[num];
				if (!CheckCellList[targetItem.ChannelNo - 1].Value)
				{
					Target.Items.Remove(targetItem);
				}
				else if (targetItem.TubeNo * targetCount > _ListAQTarget.Count - DataRowIndex || targetItem.TargetIndex > targetCount)
				{
					Target.Items.Remove(targetItem);
				}
			}
			for (int i = 0; i < CheckCellList.Count; i++)
			{
				int channelNo = i + 1;
				if (!CheckCellList[i].Value)
				{
					continue;
				}
				bool flag = true;
				for (int j = DataRowIndex; j < _ListAQTarget.Count; j++)
				{
					string text = _ListAQTarget[j].GetObject(channelNo).ToString();
					int TubeNo = _ListAQTarget[j].TubeNo;
					int TargetIndex = _ListAQTarget[j].TargetIndex;
					byte[] imageBytes = _ListAQTarget[j].GetImageBytes(channelNo);
					if (text.Length == 0)
					{
						if (showMsg)
						{
							dataGrid.UnselectAllCells();
							MessageBox.Show(Utility.GetMessage("msg_not_input_targetname"));
							dataGrid.CurrentColumn = dataGrid.Columns[i + 1];
							dataGrid.CurrentItem = _ListAQTarget[j];
							dataGrid.BeginEdit();
						}
						return false;
					}
					if (text == _StringNA)
					{
						TargetItem targetItem2 = Target.Items.Where((TargetItem s) => s.TubeNo == TubeNo && s.ChannelNo == channelNo && s.TargetIndex == TargetIndex).FirstOrDefault();
						if (targetItem2 != null)
						{
							Target.Items.Remove(targetItem2);
						}
						continue;
					}
					for (int k = j; k < _ListAQTarget.Count; k++)
					{
						for (int l = 0; l < CheckCellList.Count; l++)
						{
							if ((k != j || (k == j && l != i)) && CheckCellList[l].Value && text == _ListAQTarget[k].GetObject(l + 1).ToString())
							{
								if (showMsg)
								{
									dataGrid.UnselectAllCells();
									MessageBox.Show(Utility.GetMessage("msg_same_targetname"));
									dataGrid.CurrentColumn = dataGrid.Columns[i + 1];
									dataGrid.CurrentItem = _ListAQTarget[j];
									dataGrid.BeginEdit();
								}
								return false;
							}
						}
					}
					TargetItem targetItem3 = Target.Items.Where((TargetItem s) => s.TubeNo == TubeNo && s.ChannelNo == channelNo && s.TargetIndex == TargetIndex).FirstOrDefault();
					if (targetItem3 == null)
					{
						Target.Items.Add(new TargetItem
						{
							ChannelNo = channelNo,
							TubeNo = TubeNo,
							Name = text,
							TargetIndex = TargetIndex,
							TubeName = _ListAQTarget[j].ColumnItem.ToString(),
							Dye = RowDye.GetObject(channelNo).ToString(),
							Color = _ListAQTarget[j].GetColor(channelNo),
							ImageData = imageBytes
						});
					}
					else
					{
						targetItem3.Name = text;
						targetItem3.TubeName = _ListAQTarget[j].ColumnItem.ToString();
						targetItem3.Dye = RowDye.GetObject(channelNo).ToString();
						targetItem3.Color = _ListAQTarget[j].GetColor(channelNo);
						targetItem3.ImageData = imageBytes;
					}
					flag = false;
				}
				if (flag)
				{
					if (showMsg)
					{
						dataGrid.UnselectAllCells();
						MessageBox.Show(Utility.GetMessage("msg_not_input_targetname"));
						dataGrid.CurrentColumn = dataGrid.Columns[i + 1];
						dataGrid.CurrentItem = _ListAQTarget[DataRowIndex];
						dataGrid.BeginEdit();
					}
					return false;
				}
			}
			if (Target.Items.Count == 0)
			{
				if (showMsg)
				{
					dataGrid.UnselectAllCells();
					MessageBox.Show(Utility.GetMessage("msg_not_input_target"));
					dataGrid.CurrentColumn = dataGrid.Columns[1];
					dataGrid.CurrentItem = RowCheckBox;
					dataGrid.SelectCell(DataRowIndex, 1);
				}
				return false;
			}
			if (Type == EProjectType.AD && Target.Items.Count != 2)
			{
				if (showMsg)
				{
					MessageBox.Show(Utility.GetMessage("msg_ad_not_engouh_target"));
				}
				return false;
			}
			if (Type == EProjectType.RQ && Target.Items.Count < 2)
			{
				if (showMsg)
				{
					MessageBox.Show(Utility.GetMessage("msg_rq_not_engouh_target"));
				}
				return false;
			}
			return true;
		}

		public void SetTubeCount(int cnt)
		{
			if (cnt <= 0)
			{
				return;
			}
			dataGrid.CommitEdit();
			int num = tubeCount;
			string message = Utility.GetMessage("Prj_TubeDispaly");
			if (cnt > num)
			{
				for (int i = num; i < cnt; i++)
				{
					int tubeNo = i + 1;
					_ListAQTarget.Add(new AQTargetDataRow(message + (i + 1), "", "", "", "", "", "", "", "")
					{
						TubeNo = tubeNo,
						TargetIndex = 1
					});
					for (int j = 1; j < targetCount; j++)
					{
						_ListAQTarget.Add(new AQTargetDataRow(message + (i + 1), "", "", "", "", "", "", "", "")
						{
							TubeNo = i + 1,
							TargetIndex = j + 1
						});
					}
				}
				SetTubeTargetName();
			}
			else if (num > cnt)
			{
				for (int num2 = num; num2 > cnt; num2--)
				{
					int num3 = num2 * targetCount + DataRowIndex - 1;
					for (int k = 0; k < targetCount; k++)
					{
						_ListAQTarget.RemoveAt(num3--);
					}
				}
			}
			tubeCount = cnt;
		}

		private void SetTubeTargetName()
		{
			for (int i = DataRowIndex; i < _ListAQTarget.Count; i++)
			{
				foreach (GridCheckBoxCell checkCell in CheckCellList)
				{
					if (checkCell.Value && string.IsNullOrEmpty(_ListAQTarget[i].GetObject(checkCell.Index).ToString()))
					{
						_ListAQTarget[i].SetValue(checkCell.Index, _ListAQTarget[i].TubeNo + "-" + checkCell.Index + ((!isMutilpleTarget) ? "" : ("-" + _ListAQTarget[i].TargetIndex)));
					}
				}
			}
		}

		public void SetTargetCount(int cnt)
		{
			if (cnt <= 0)
			{
				return;
			}
			dataGrid.CommitEdit();
			int num = targetCount;
			string message = Utility.GetMessage("Prj_TubeDispaly");
			if (cnt > num)
			{
				int i;
				for (i = 0; i < tubeCount; i++)
				{
					AQTargetDataRow aQTargetDataRow = _ListAQTarget.ToList().FindLast((AQTargetDataRow s) => s.TubeNo == i + 1);
					int num2 = _ListAQTarget.IndexOf(aQTargetDataRow);
					if (aQTargetDataRow != null && num2 >= 0)
					{
						int num3 = aQTargetDataRow.TargetIndex;
						for (int j = num; j < cnt; j++)
						{
							num2++;
							num3++;
							_ListAQTarget.Insert(num2, new AQTargetDataRow(message + (i + 1), "", "", "", "", "", "", "", "")
							{
								TubeNo = i + 1,
								TargetIndex = num3
							});
						}
					}
				}
				SetTubeTargetName();
			}
			else if (num > cnt)
			{
				int k;
				for (k = 0; k < tubeCount; k++)
				{
					int num4 = _ListAQTarget.ToList().FindLastIndex((AQTargetDataRow s) => s.TubeNo == k + 1);
					if (num4 >= 0)
					{
						int num5 = num;
						while (num5 > cnt && num4 != 0)
						{
							_ListAQTarget.RemoveAt(num4);
							num4--;
							num5--;
						}
					}
				}
			}
			targetCount = cnt;
		}

		public void UnSelectAll()
		{
			dataGrid.UnselectAllCells();
		}

		private void DataGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
		{
			if (IsReadOnly)
			{
				e.Cancel = true;
			}
			else if (e.Column.DisplayIndex == 0)
			{
				if (e.Row.GetIndex() < DataRowIndex)
				{
					e.Cancel = true;
				}
			}
			else if (!CheckCellList[e.Column.DisplayIndex - 1].IsEnable)
			{
				e.Cancel = true;
			}
			else if (e.Row.Item == RowCheckBox)
			{
				e.Cancel = true;
			}
			else if (!CheckCellList[e.Column.DisplayIndex - 1].Value)
			{
				e.Cancel = true;
			}
			if (!e.Cancel && e.Row.Item == RowDye)
			{
				RefreshChannelDyes(e.Column.DisplayIndex - 1);
			}
		}

		private void RefreshChannelDyes(int index)
		{
			int[] array = new int[8];
			for (int i = 0; i < 8; i++)
			{
				string dye = RowDye.GetObject(i + 1).ToString();
				array[i] = _ChannelSetting.AllChannelProperty.IndexOf(_ChannelSetting.AllChannelProperty.Where((ChannelProperty s) => s.DyeList.Contains(dye)).FirstOrDefault());
			}
			ChannelDyes[index].Clear();
			int num = 0;
			int j;
			for (j = 0; j < _ChannelSetting.AllChannelProperty.Count; j++)
			{
				if (array[index] != j && array.Where((int s) => s == j).Count() > 0)
				{
					continue;
				}
				num++;
				foreach (string dye2 in _ChannelSetting.AllChannelProperty[j].DyeList)
				{
					if (!string.IsNullOrEmpty(dye2))
					{
						ChannelDyes[index].Add(new DyeItem
						{
							DyeName = dye2,
							Category = (num.ToString() ?? "")
						});
					}
				}
			}
		}

		private void SelectImageCommandExecute(object sender, ExecutedRoutedEventArgs e)
		{
			Button button = e.OriginalSource as Button;
			if (button != null)
			{
				AQTargetDataRow aQTargetDataRow = e.Parameter as AQTargetDataRow;
				OpenFileDialog openFileDialog = new OpenFileDialog();
				openFileDialog.Filter = string.Format("{0}{1}|*.*", "*.jpg;*.jpeg;*.png;", Utility.GetMessage("File"));
				openFileDialog.FileName = string.Empty;
				openFileDialog.FilterIndex = 1;
				openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
				if (openFileDialog.ShowDialog() == true)
				{
					byte[] bytes;
					aQTargetDataRow.SetImage(Convert.ToInt32(button.Tag), Utility.LoadImage(openFileDialog.FileName, out bytes), bytes);
				}
			}
		}

		private void Border_MouseEnter(object sender, MouseEventArgs e)
		{
			Border border = sender as Border;
			popup.PlacementTarget = border;
			popup.IsOpen = true;
			if (border.Tag != null)
			{
				showImage.Source = border.Tag as BitmapImage;
			}
		}

		private void Border_MouseLeave(object sender, MouseEventArgs e)
		{
			popup.IsOpen = false;
			showImage.Source = null;
		}

		private void Border_MouseRightButtonDown1(object sender, MouseButtonEventArgs e)
		{
			DeleteImage(e.OriginalSource, 1);
		}

		private void Border_MouseRightButtonDown2(object sender, MouseButtonEventArgs e)
		{
			DeleteImage(e.OriginalSource, 2);
		}

		private void Border_MouseRightButtonDown3(object sender, MouseButtonEventArgs e)
		{
			DeleteImage(e.OriginalSource, 3);
		}

		private void Border_MouseRightButtonDown4(object sender, MouseButtonEventArgs e)
		{
			DeleteImage(e.OriginalSource, 4);
		}

		private void Border_MouseRightButtonDown5(object sender, MouseButtonEventArgs e)
		{
			DeleteImage(e.OriginalSource, 5);
		}

		private void Border_MouseRightButtonDown6(object sender, MouseButtonEventArgs e)
		{
			DeleteImage(e.OriginalSource, 6);
		}

		private void Border_MouseRightButtonDown7(object sender, MouseButtonEventArgs e)
		{
			DeleteImage(e.OriginalSource, 7);
		}

		private void Border_MouseRightButtonDown8(object sender, MouseButtonEventArgs e)
		{
			DeleteImage(e.OriginalSource, 8);
		}

		private void DeleteImage(object source, int index)
		{
			Image image = source as Image;
			if (image != null)
			{
				AQTargetDataRow aQTargetDataRow = image.Tag as AQTargetDataRow;
				if (MessageBox.Show(Utility.GetMessage("Prj_Image_Delete_Msg"), Utility.GetMessage("Warning"), MessageBoxButton.YesNo, MessageBoxImage.Exclamation) == MessageBoxResult.Yes)
				{
					aQTargetDataRow.RemoveImage(index);
				}
			}
		}
	}
}
